package gupao.concurrency.ratelimit;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Semaphore;

@Slf4j
public class SemaphoreRateLimiter {
    private final Semaphore semaphore;

    public SemaphoreRateLimiter(int permits){
        semaphore = new Semaphore(permits);
    }

    public boolean tryAcquire(){
        boolean rt = semaphore.tryAcquire();
        log.info("semaphore tryAcquire {}, availablePermits: {}", rt, semaphore.availablePermits());
        return rt;
    }

    public void release(){
        semaphore.release();
        log.info("semaphore release, availablePermits: {}", semaphore.availablePermits());
    }
}

